package test.test3打印奇数;

public class Number implements Runnable{
    //第二种方式实现多线程，测试类中MyRunable只创建一次，所以不需要加static
    int number = 1;
    @Override
    public void run() {
        //循环
        while (true){
            //同步代码块
            synchronized (Number.class){
                //判断是否到共享数据末尾
                if (number > 100){//到了
                    break;
                }else {//没到
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    //判断是否为奇数
                    if (number % 2 == 1){//为奇数
                        System.out.println(Thread.currentThread().getName()+"打印了数字"+number);
                    }
                    number ++;
                }
            }
        }
    }
}
